package leetcode_core.leetcode_3;

import java.util.ArrayList;
import java.util.List;

public class IntervalIntersection {
    public int[][] intervalIntersection(int[][] firstList, int[][] secondList) {
        //1.排序,已经做好了,只需要双指针即可
        List<int[]> list = new ArrayList<>();//都是从尾部插入,数组的最快了
        int p1 = 0,p2 = 0;
        while (p1<firstList.length && p2<secondList.length){
            //如果有交集
            int a1 = firstList[p1][0],a2=firstList[p1][1];
            int b1 = secondList[p2][0],b2=secondList[p2][1];
            if(b2>=a1 && a2>=b1){
                list.add(new int[]{Math.max(a1,b1),Math.min(a2,b2)});
            }
            if(b2 < a2){
                p2++;
            }else{
                p1++;
            }
        }
        return list.toArray(new int[0][]);
    }
}
